/**
 * Created by Administrator on 2017/7/6.
 */
$(document).ready(function(){
    $("#login").click(function(){
        var user = $("#username").val();
        var pwd = $("#password").val();
        print(user)
        print(pwd)
        var pd = {"username":user, "password":pwd};
        $.ajax({
            type:"post",
            url:"/",
            data:pd,
            cache:false,
            success:function(data){
                if(data.status==0) {
                    window.location.href = "/user?user=" + user;
                }
                else if(data.status==3)
                    window.location.href = "/room?room_id="+data.msg;
                else
                    alert(data.msg)
            },
            error:function(){
                alert("error!");
            },
        });
    });
    $("#register").click(function(){
        var user = $("#username").val();
        var pwd = $("#password").val();
        var pd = {"username":user, "password":pwd};
        $.ajax({
            type:"post",
            url:"/register",
            data:pd,
            cache:false,
            success:function(data){
                alert("注册成功！")
            },
            error:function(){
                alert("error!");
            },
        });
    });
    $("#joinRoom").click(function(){
        var roomId = $("#room_id").val();
        var pd = {"roomId":roomId};
        $.ajax({
            type:"post",
            url:"/joinRoom",
            data:pd,
            cache:false,
            success:function(data){
                if(data.status==0)
                    window.location.href = "/room?room_id="+data.msg;
            },
            error:function(){
                alert("加入失败!");
            },
        });
    });
});
function createNewRoom() {
    var user = $("#username").val();
    var pwd = $("#password").val();
    var pd = {"username":user, "password":pwd};
    $.ajax({
        type:"post",

        url:"/createRoom",
        data:pd,
        cache:false,
        success:function(data){
            if(data.status==0)
                    window.location.href = "/room?room_id="+data.msg;
                else
                    alert(data.msg)
        },
        error:function(data){
            alert(data.msg);
        },
    });
}